import { Injectable } from '@angular/core';
import {RequestUtil} from "../../../util/request-util";
import {API} from "../../../constants/constants";
import {Observable} from "rxjs";
import {MsgResult} from "../../../models/msg-result";
import {FormUtil} from "../../../util/form-util";

@Injectable()
export class AccountsService {

  constructor(
    private formUtil :FormUtil,
    private requestUtil :RequestUtil
  ) { }

  /**
   * 添加账户
   * @param account : 对象实例
   * @returns {Observable<MsgResult>|any}
   */
  insertAccount(account):Observable<MsgResult>{
    let params="accountName="+account.accountName+"&password="+account.password+"&isEnable="+account.isEnable+
      "&isAudit="+account.isAudit+"&isAllowPc="+account.isAllowPc+"&isAllowApp="+account.isAllowApp+"&isUse="+"0";
    return this.requestUtil.post(API.add,params);
  }

  /**
   * 删除账户
   * @param id :账户id
   * @returns {Observable<MsgResult>|any}
   */
  deleteAcount(id):Observable<MsgResult>{
    return this.requestUtil.delete(API.delete+"/"+id+"?format=json");
  }

  deleteAcounts(ids):Observable<MsgResult>{
    let params = 'accountIds='+ids;
    return this.requestUtil.post(API.deleteAccounts,params);
  }

  /**
   * 根据id查询
   * @request :http://localhost:8080/one-admin/api/accounts/123?format=json
   * @param id:账号id；
   * @returns {Observable<MsgResult>|any}
   */
  getAccountById(id):Observable<MsgResult>{
    return this.requestUtil.get(API.getById+"/"+id+"?format=json");
  }

  /**
   * 修改账户   +"?format=json"+
   *
   * @param account
   * @returns {Observable<MsgResult>|any}
   */
  updateAccount(account):Observable<MsgResult>{
    let params = this.formUtil.serialize(account);
    return this.requestUtil.put(API.edit+"/"+account.id+"?format=json",params);
  }

  /**
   * 修改密码
   * @param account
   * @returns {Observable<MsgResult>|any}
   */
  editPwd(account):Observable<MsgResult>{
    let id=account.id;
    let params="&originalPwd="+account.password+"&newPwd="+account.newPassword+"&accountName="+account.accountName;
    return this.requestUtil.put(API.editPwd+"/"+id+"/pwd/edit?format=json"+params);
  }

  /**
   * 密码重置
   * @returns {null}
   */
  resetPwd(id):Observable<MsgResult>{
    let params="tag="+"1";
    return this.requestUtil.put(API.resetPwd+"/"+id+"/pwd/reset?format=json&"+params);
  }

  /**
   * 为账户分配角色
   * @param id
   * @param rolesIds
   * @returns {Observable<MsgResult>|any}
   */
  assignRoles(id,rolesIds):Observable<MsgResult>{
    let params="roles="+rolesIds;
    return this.requestUtil.post(API.assignRoles+"/"+id+"/roles/assgin?format=json&"+params);
  }

  /**
   * 初始化并查询分配的权限
   * @param id
   * @returns {Observable<MsgResult>|any}
   */
  initAssignAuthority(id){
    return this.requestUtil.post(API.initAssignAuthority+"/"+id+"/authority/assign/init?format=json&");
}

  /**
   * 为账号分配权限
   * @param authoritys
   * @param authorityConfigs
   * @returns {Observable<MsgResult>|any}
   */
  assignAuhtoritys(id,authoritys,authorityConfigs):Observable<MsgResult>{
    let params="authoritys="+authoritys+"&authorityConfigs="+authorityConfigs;
    return this.requestUtil.post(API.assignAuhtoritys+"/"+id+"/authoritys/assign?format=json&"+params);
  }

  /**
   * 获取未分配账户
   * @param id
   * @param authoritys
   * @param authorityConfigs
   * @returns {Observable<MsgResult>|any}
   */
  getUndistributionRoleAccount(roleId):Observable<MsgResult>{
    return this.requestUtil.post(API.getUndistributionRoleAccount+"/"+roleId+"/Account/Undistribution?format=json&");
  }

  /**
   * 获取未分配角色
   * @param accountId
   * @returns {Observable<MsgResult>|any}
   */
  getUndistributedRoleByAccId(accountId):Observable<MsgResult>{
    return this.requestUtil.post(API.getUndistributedRoleByAccId+"/"+accountId+"/Role/Undistributed?format=json");
  }
  /**
   * 获取已分配角色
   * @param accountId
   * @returns {Observable<MsgResult>|any}
   */
  getAssignedRoleByAccId(accountId):Observable<MsgResult>{
    return this.requestUtil.post(API.getAssignedRoleByAccId+"/"+accountId+"/Role/Assigned?format=json");
  }
  /**
   * 快捷操作提交
   */
  postAccountSpeedySet(flag,value,accountId):Observable<MsgResult>{
    let params = "flag="+flag+"&"+flag+"="+value;
    return this.requestUtil.post(API.accountSpeedySet+"/"+accountId+"/speedy/settings?format=json",params);
  }

  /**
   * 获取主题
   * @returns {Observable<MsgResult>|any}
   */
  getThemes():Observable<MsgResult>{
    return this.requestUtil.get(API.getThemeInfos);
  }

  /**
   * 保存为账户批量设置的主题
   * @param accountId
   * @param themeId
   * @returns {Observable<MsgResult>|any}
   */
  postAccountThemes(accountId,themeId):Observable<MsgResult>{
    let params = "accountIdStr="+accountId+"&themeId="+themeId;
    return this.requestUtil.post(API.postAccountThemes,params);
  }

  /**
   * 获取门户
   * @returns {Observable<MsgResult>|any}
   */
  getPortal():Observable<MsgResult>{
    return this.requestUtil.get(API.getPortals);
  }

  /**
   * 保存为账户批量设置的门户
   * @param accountId
   * @param portalId
   * @returns {Observable<MsgResult>|any}
   */
  postAccountPortal(accountId,portalId):Observable<MsgResult>{
    let params = "accountIdStr="+accountId+"&portalId="+portalId;
    return this.requestUtil.post(API.postAccountPortal,params);
  }

  /**
   * 为账户分配首页获取菜单树
   * @returns {Observable<MsgResult>|any}
   */
  getMenusForAccount():Observable<MsgResult>{
    return this.requestUtil.get(API.getMenusForAccount);
  }

  /**
   * 为账户分配首页获取菜单树
   * @returns {Observable<MsgResult>|any}
   */
  settingsHomePage(accountIdArr,menuUri,menuId):Observable<MsgResult>{
    let params = "accountIdStr="+accountIdArr+"&uri="+menuUri+"&menuId="+menuId;
    return this.requestUtil.post(API.settingsHomePage,params);
  }

  /**
   *  个人设置主题
   * @param themeId
   * @returns {Observable<MsgResult>}
   */
  setTheme(themeId):Observable<MsgResult>{
    return this.requestUtil.post(API.setTheme+"/"+themeId+"/theme/set");
  }

  /**
   * 个人设置门户
   * @param portalId
   * @returns {Observable<MsgResult>}
   */
  setPortal(portalId):Observable<MsgResult>{
    return this.requestUtil.post(API.setPortal+"/"+portalId+"/theme/set");
  }



}
